# coding=utf-8
# 初始化数据库
import sys
import os
import pexpect

mysql_url = "192.168.56.111"
mysql_port = 3307
mysql_db_name = "beetle-auth"
mysql_username = "root"
mysql_password = "root"

project_root = os.path.abspath(os.path.dirname(__file__))
base_sql_file=os.path.join(project_root, 'base_sql_file.sql')

def creata_database():
    if mysql_url == "":
        print "ERROR : mysql url is none "
        return
    print "=====create database start====="
    sh = '/bin/bash -c "mysql -h%s -P%s -u%s -p%s %s < %s --default-character-set=utf8"' % (
        mysql_url,mysql_port,mysql_username, mysql_password, mysql_db_name, base_sql_file)
    print '  start execute shell : %s' % sh
    output, status = pexpect.run(sh, withexitstatus = 1, timeout = 60)
    print '  shell status: %s, output: %s' % (status, output)
    if status == 0:
        print '  Import data success.'
    else:
        print '  Import data failure, cause: %s.' % output
    print "=====create database end====="

if __name__ == "__main__":
    reload(sys)
    sys.setdefaultencoding('utf8')
    try:
        mysql_url = sys.argv[1]
        mysql_port = sys.argv[2]
        mysql_db_name = sys.argv[3]
        mysql_username = sys.argv[4]
        mysql_password = sys.argv[5]
    except IndexError as e:
        pass

    creata_database()
